package com.navi.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on 2017/6/21.
 */
public class NaviTimeUtil {
    public static Double getDoubleTime() {



        long now = Instant.now().toEpochMilli(); // 微秒级当前时间
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(now);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DATE);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        int millisecond = c.get(Calendar.MILLISECOND);


        Double days = Double.valueOf("0");// 统计天数
        for (int i = 1900; i < year; i++) {
            days += getDaysByYear(i);
        }
        int[] days_each_month = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30,
                31, 30, 31};

        if (isLeepYear(year)) {
            days_each_month[1] = 29;
        }
        for (int i = 0; i < month - 1; i++) {
            days += days_each_month[i];
        }
        days = days + day + 1;

        double seconds = hour * 3600 + minute * 60 + second;
        double millSeconds = millisecond + seconds * 1000;
        Double d = Double.valueOf(millSeconds / (3600 * 24 * 1000) + "");
        return Double.valueOf(days + d);
    }

    public static String getTimestampByDblTime(double dTime) {
        int alldays = (int) dTime, year = 0, mon = 0, day = 0, hour, minute, second;
        double allFloatPartDay = dTime - alldays;// double时间的小数部分
        int remainDays = alldays - 1;
        StringBuffer timeStamp = new StringBuffer();
        timeStamp.setLength(0);
        // get year & mon
        Calendar c = Calendar.getInstance();
        for (int i = 1900; i < c.get(Calendar.YEAR) + 1; i++) {
            if (remainDays <= getDaysByYear(i)) {//remainDays<=当年的天数，那么year=i
                year = i;
                break;
            }
            remainDays -= getDaysByYear(i);
        }
        int[] days_each_month = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (isLeepYear(year)) {
            days_each_month[1] = 29;
        }
        //get day
        int days = 0;
        for (int i = 0; i < 12; i++) {
            if ((days + days_each_month[i]) >= remainDays) {
                mon = i + 1;
                day = remainDays - days;
                break;
            }
            days += days_each_month[i];
        }
        // get hour,minute,second
        hour = (int) (24 * allFloatPartDay);
        minute = (int) (24 * 60 * allFloatPartDay - 60 * (hour));
        second = (int) (24 * 60 * 60 * (allFloatPartDay) - 60 * minute - 60 * 60 * hour);
        timeStamp.append(year).append("-").append(comInt2String(checkTimeMaxVal(mon, "MON"), 2))
                .append("-").append(comInt2String(checkTimeMaxVal(day, "DAY"), 2)).append(" ")
                .append(comInt2String(checkTimeMaxVal(hour, "HOUR"), 2)).append(":")
                .append(comInt2String(checkTimeMaxVal(minute, "MINUTE"), 2)).append(":")
                .append(comInt2String(checkTimeMaxVal(second, "SECOND"), 2));
        return timeStamp.toString();
    }

    public static int checkTimeMaxVal(int time, String type) {
        if (("MON").equals(type) && time > 12) {
            return 0;
        }
        if (("DAY").equals(type) && time > 31) {
            return 0;
        }
        if (("HOUR").equals(type) || ("MINUTE").equals(type)
                || ("SECOND").equals(type)) {
            if (time >= 60) {
                return 0;
            }
        }
        return time;
    }

    public static String comInt2String(int value, int length) {
        return String.format("%02d", value);
    }

    // 获得某年的天数
    private static int getDaysByYear(int year) {
        if (isLeepYear(year)) {
            return 366;
        } else {
            return 365;
        }
    }

    public static Long timeToLang(String time){
        Long longTime = 0L;
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
            longTime = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return longTime;
    }

    // 判断是否是闰年
    private static boolean isLeepYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    public static void main(String[] args){
        double dTime = getDoubleTime();
        System.out.println("" + dTime + "\n");
        System.out.println("" + getTimestampByDblTime(dTime) + "\n");
    }
}
